﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Common;
using DLMemberPointMS.Entity.ViewModel;
using System.Data.SqlClient;

namespace DLMemberPointMS.Business
{
    public class MemCardsService : BaseService<MemCards>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public OperateResult GetData(MemCardsModel mod)
        {
            var query = PredicateExtensions.True<MemCards>();
            //拼接条件
            if (!string.IsNullOrEmpty(mod.MC_CardID))
            {
                query = query.And(m => m.MC_CardID.Contains(mod.MC_CardID));
            }
            if (!string.IsNullOrEmpty(mod.MC_Name))
            {
                query = query.And(m => m.MC_Name.Contains(mod.MC_Name));
            }
            if (!string.IsNullOrEmpty(mod.MC_Mobile))
            {
                query = query.And(m => m.MC_Mobile.Contains(mod.MC_Mobile));
            }
            if (mod.CL_ID != 0 && mod.CL_ID != null)
            {
                query = query.And(m => m.CL_ID == mod.CL_ID);
            }
            if (mod.MC_State != 0 && mod.MC_State != null)
            {
                query = query.And(m => m.MC_State == mod.MC_State);
            }
            //数据的行数
            int count = 0;
            var datas = GetList(mod.page, mod.limit, ref count, query, c => c.MC_ID, true).Select(c => new
            {
                c.MC_CardID,
                c.MC_Name,
                c.MC_Mobile,
                c.MC_TotalMoney,
                c.MC_State,
                c.MC_Point,
                c.MC_Sex,
                c.MC_CreateTime,
                c.MC_Money,
                c.CardLevels.CL_LevelName
            });
            return new OperateResult(code.Success, "ok", datas, count);
        }
        /// <summary>
        /// 添加会员
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public OperateResult AddMemCard(MemCardSEditViewModel mod)
        {
            if (Find(c => c.MC_CardID == mod.MC_CardID) == null)
            {
                MemCards model = new MemCards();
                model.CL_ID = mod.CL_ID;
                model.MC_BirthdayType = mod.MC_BirthdayType;
                model.MC_Birthday_Day = mod.MC_Birthday_Day;
                model.MC_Birthday_Month = mod.MC_Birthday_Month;
                model.MC_CardID = mod.MC_CardID;
                model.MC_CreateTime = DateTime.Now.ToLocalTime();
                model.MC_ID = mod.MC_ID;
                model.MC_IsPast = (bool)mod.MC_IsPast;
                model.MC_IsPointAuto = (bool)mod.MC_IsPointAuto;
                model.MC_Mobile = mod.MC_Mobile;
                model.MC_Money = mod.MC_Money;
                model.MC_Name = mod.MC_Name;
                model.MC_State = 0;
                model.MC_TotalCount = 0;
                model.MC_TotalMoney = 0;
                model.MC_RefererCard = "";
                model.MC_RefererID = 0;
                model.MC_RefererName = mod.MC_RefererName;
                model.S_ID = 1;
                model.MC_OverCount = 0;
                model.MC_State = mod.MC_State;
                if (!string.IsNullOrWhiteSpace(mod.MC_Password_confirm) && !string.IsNullOrWhiteSpace(mod.MC_Password))
                {
                    if (!mod.MC_Password.Equals(mod.MC_Password_confirm))
                    {
                        return new OperateResult(code.Error, "两次输入密码不一致");
                    }
                    model.MC_Password = mod.MC_Password;
                }
                model.MC_PastTime = (DateTime)mod.MC_PastTime;
                model.MC_Point = mod.MC_Point;
                if (!string.IsNullOrWhiteSpace(mod.MC_RefererCard))
                {
                    var referer = Find(e => e.MC_RefererCard == mod.MC_RefererCard);
                    if (referer != null)
                    {
                        model.MC_RefererCard = mod.MC_RefererCard;
                        model.MC_RefererID = mod.MC_RefererID;
                    }
                    else
                    {
                        return new OperateResult(code.Error, "您输入的推荐人不存在");
                    }
                }
                model.MC_Sex = mod.MC_Sex;
                model.S_ID = mod.S_ID;
                model.CL_ID = mod.CL_ID;
                if (Add(model))
                {
                    return new OperateResult(code.Success, "添加成功");
                }
                else
                {
                    return new OperateResult(code.Error, "出错了，请重试");
                }
            }
            return new OperateResult(code.Error, "此卡号已存在");
        }
        /// <summary>
        /// 修改会员信息
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public OperateResult EditMemCard(MemCardSEditViewModel mod)
        {
            //if (Find(c => c.MC_CardID == mod.MC_CardID) == null)
            //{
            var model = Find(c => c.MC_CardID == mod.MC_CardID);

            // MemCards model = new MemCards();
            model.CL_ID = mod.CL_ID;
            model.MC_BirthdayType = mod.MC_BirthdayType;
            model.MC_Birthday_Day = mod.MC_Birthday_Day;
            model.MC_Birthday_Month = mod.MC_Birthday_Month;
            model.MC_CardID = mod.MC_CardID;
            //      model.MC_CreateTime = mod.MC_CreateTime;
            model.MC_ID = mod.MC_ID;
            model.MC_IsPast = (bool)mod.MC_IsPast;
            model.MC_IsPointAuto = (bool)mod.MC_IsPointAuto;
            model.MC_Mobile = mod.MC_Mobile;
            model.MC_Money = mod.MC_Money;
            model.MC_Name = mod.MC_Name;
            model.MC_State = mod.MC_State;
            // MC_OverCount = mod.MC_OverCount;
            //MC_Password = mod.MC_Password;
            if (!string.IsNullOrWhiteSpace(mod.MC_Password_confirm) && !string.IsNullOrWhiteSpace(mod.MC_Password))
            {
                if (!mod.MC_Password.Equals(mod.MC_Password_confirm))
                {
                    return new OperateResult(code.Error, "两次输入密码不一致");
                }
            }
            model.MC_Password = mod.MC_Password;
            model.MC_PastTime = mod.MC_PastTime;
            //MC_Photo = mod.MC_Photo;
            model.MC_Point = mod.MC_Point;
            //MC_RefererCard = mod.MC_RefererCard;
            //MC_RefererID = mod.MC_RefererID;
            //model.MC_RefererName = mod.MC_RefererName;
            if (!string.IsNullOrWhiteSpace(mod.MC_RefererCard))
            {
                var referer = Find(e => e.MC_RefererCard == mod.MC_RefererCard);
                if (referer != null)
                {
                    model.MC_RefererCard = mod.MC_RefererCard;
                    model.MC_RefererID = mod.MC_RefererID;
                }
                else
                {
                    return new OperateResult(code.Error, "您输入的推荐人不存在");
                }
            }
            //}
            model.MC_Sex = mod.MC_Sex;
            //MC_State = mod.MC_State;
            //MC_TotalCount = mod.MC_TotalCount;
            //MC_TotalMoney = mod.MC_TotalMoney;
            model.S_ID = mod.S_ID;
            if (Update(model))
            {
                return new OperateResult(code.Success, "修改成功");
            }
            else
            {
                return new OperateResult(code.Error, "出错了，请重试");
            }

        }
        /// <summary>
        /// 删除会员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult DeleteMember(string id)
        {
            var data = Find(m => m.MC_CardID == id);
            if (data != null)
            {
                if (Delete(data))
                {
                    return new OperateResult(code.Success, "删除成功");
                }
                else
                {
                    return new OperateResult(code.Error, "删除失败");
                }
            }
            else
            {
                return new OperateResult(code.Error, "请重试");
            }

        }
        /// <summary>
        /// 查询会员卡状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MemCardSEditViewModel ChangeState(string id)
        {
            MemCards MemberModel = Find(m => m.MC_CardID == id);
            MemCardSEditViewModel viewModel = new MemCardSEditViewModel
            {
                MC_CardID = MemberModel.MC_CardID,
                MC_State = MemberModel.MC_State
            };
            return viewModel;
        }
        /// <summary>
        /// 修改卡状态
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public OperateResult ChangeStateResult(MemCardSEditViewModel mod)
        {
            var MemModel = Find(m => m.MC_CardID == mod.MC_CardID);
            try
            {
                MemModel.MC_State = mod.MC_State;
            }
            catch (Exception)
            {

                return new OperateResult(code.Error, "出错了，请重试");
            }

            if (Update(MemModel))
            {
                return new OperateResult(code.Success, "修改成功");
            }
            else
            {
                return new OperateResult(code.Error, "出错了，请重试");
            }

        }
        /// <summary>
        /// 显示转出会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MemCardSEditViewModel TransferOut(string id)
        {
            var model = Find(m => m.MC_CardID == id);
            var datas = new MemCardSEditViewModel
            {
                MC_CardID = model.MC_CardID,
                MC_Name = model.MC_Name,
                MC_Point = model.MC_Point,
                MC_TotalMoney = model.MC_TotalMoney,
                MC_Money = model.MC_Money
            };
            return datas;
        }
        /// <summary>
        /// 显示转入会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult TransferIn(string id)
        {
            string Card_ID = id.Trim();
            var model = Find(m => m.MC_CardID == Card_ID);
            if (model == null)
            {
                return new OperateResult(code.Error, "用户不存在");
            }
            var datas = new MemCardSEditViewModel
            {
                MC_CardID = model.MC_CardID,
                MC_Name = model.MC_Name,
                MC_Point = model.MC_Point,
                MC_TotalMoney = model.MC_TotalMoney,
                MC_Money = model.MC_Money
            };
            return new OperateResult(code.Success, "ok", datas);
        }
        /// <summary>
        /// 开始转账
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public OperateResult StartTransfer(MemCardSEditViewModel mod)
        {

            if (mod.UserID == 0)
            {
                return new OperateResult(code.Error, "用户ID出错，请重新登录");
            };
            if (string.IsNullOrWhiteSpace(mod.outID))
            {
                return new OperateResult(code.Error, "转账人卡号为空");
            };
            if (string.IsNullOrWhiteSpace(mod.inID))
            {
                return new OperateResult(code.Error, "被转账人卡号为空");
            }
            if (mod.tranmoney == 0)
            {
                return new OperateResult(code.Error, "转账金额为0");
            }
            if (string.IsNullOrWhiteSpace(mod.remark))
            {
                mod.remark = "(没有备注)";
            }
            SqlParameter[] para =
            {
                new SqlParameter("@U_ID",mod.UserID),
                new SqlParameter("@inID",mod.inID),
                new SqlParameter("@outID",mod.outID),
                new SqlParameter("@money",mod.tranmoney),
                new SqlParameter("@remark",mod.remark),
                new SqlParameter("@outmsg",System.Data.SqlDbType.VarChar,128)
            };
            para[5].Direction = System.Data.ParameterDirection.Output;
            try
            {
                db.Database.ExecuteSqlCommand("exec Proc_TransferMoney  @inID ,@outID ,@money,@U_ID, @remark,@outmsg output", para);
                if (!string.IsNullOrWhiteSpace(para[5].Value.ToString()))
                {
                    return new OperateResult(code.Error, para[5].Value.ToString());
                }
                else
                {
                    return new OperateResult(code.Success, "转账成功");
                }
            }
            catch (Exception ex)
            {

                return new OperateResult(code.Error, ex.ToString());
            }
        }
        /// <summary>
        /// 会员换卡查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperateResult ChangeCardView(string id)
        {
            var mod = Find(m => m.MC_CardID == id);
            if (mod != null)
            {
                var viewMod = new MemCardSEditViewModel();
                viewMod.MC_CardID = mod.MC_CardID;
                viewMod.MC_Name = mod.MC_Name;
                viewMod.MC_CreateTime = (DateTime)mod.MC_CreateTime;
                viewMod.CL_LevelName = mod.CardLevels.CL_LevelName;
                return new OperateResult(code.Success, "ok", viewMod);
            }
            else
            {
                return new OperateResult(code.Error, "查询失败");
            }
        }

        public OperateResult ChangeCardRes(MemCardSEditViewModel viewmod)
        {
            var memberModel = Find(m => m.MC_CardID == viewmod.MC_CardID);
            if (memberModel.MC_Password == viewmod.MC_Password)
            {
                if (viewmod.MC_New_Password != viewmod.MC_Password_confirm)
                {
                    return new OperateResult(code.Error, "两次输入密码不一致！");
                }
                if (Find(m => m.MC_CardID == viewmod.MC_New_CardID) == null)
                {
                    memberModel.MC_CardID = viewmod.MC_New_CardID;
                    memberModel.MC_Password = viewmod.MC_New_Password;
                    if (Update(memberModel))
                    {
                        return new OperateResult(code.Success, "修改成功,新的会员卡号为:" + viewmod.MC_New_CardID);
                    }
                    else
                    {
                        return new OperateResult(code.Error, "修改失败，请重试。");
                    }
                }
                else
                {
                    return new OperateResult(code.Error, "指定的会员卡号已存在，请更换。");
                }

            }
            else
            {
                return new OperateResult(code.Error, "密码错误");
            }
        }

        public OperateResult UploadImgSaveDb(string path)
        {

            return new OperateResult(code.Success, "ok");
        }
    }
}
